<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="填写人姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入填写人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交时间" prop="submitTime">
        <el-date-picker clearable
          v-model="queryParams.submitTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择提交时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search"  @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:answer:add']"
        >新增练习</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"

          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:answer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:answer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <SmartTable v-loading="loading" :data="answerList" rowKey="key" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="试卷" align="center" prop="paperTitle" />
      <el-table-column label="填写人账号" align="center" prop="userCode" />
      <el-table-column label="填写人姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180" />
      <el-table-column label="分数" align="center" prop="score" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" @click="handleDetail(scope.row)">查看</el-button>
          <el-button type="text" v-hasPermi="['system:answer:score']" @click="handleScore(scope.row)">打分</el-button>
        </template>
      </el-table-column>
    </SmartTable>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog title="答题详情" v-model="detailModal" width="1000px" append-to-body>
      <el-descriptions title="基本信息">
        <el-descriptions-item label="填写人账号">{{ detailInfo.userCode }}</el-descriptions-item>
        <el-descriptions-item label="填写人名称">{{ detailInfo.userName }}</el-descriptions-item>
        <el-descriptions-item label="提交时间">{{ detailInfo.submitTime }}</el-descriptions-item>
        <el-descriptions-item label="试卷">{{ detailInfo.paperTitle }}</el-descriptions-item>
      </el-descriptions>
      <el-card shadow="never">
        <template #header>答题明细</template>
        <div style="max-height: 650px;overflow-y: auto; overflow-x: hidden">
          <ol>
            <li v-for="(item, index) in detailInfo.examQuestionList" :key="index + 'q'" class="question-item">
              <div class="question-title">
                {{ item.questionText }}({{ item.score }}分)
                <span v-if="item.examAnswer" style="float: right; font-size: 14px">
                  得分: {{ item.examAnswer.score }}分
                </span>
              </div>
              <div v-if="item.questionType === 'CHOICE'">
                <el-radio-group v-model="item.selectedOptionId" disabled>
                  <el-radio v-for="(option, i) in item.examQuestionOptionList" :key="option.id" :value="option.id">
                    {{ ['A', 'B', 'C', 'D', 'E'][i]}}.
                    {{ option.optionText }}
                    <span v-if="option.isCorrect" class="color-green">(正确答案)</span>
                  </el-radio>
                </el-radio-group>
              </div>
              <div v-if="item.questionType === 'INPUT'">
                &nbsp;{{ item.inputContent }}
              </div>
            </li>
          </ol>
        </div>
      </el-card>
    </el-dialog>

    <el-dialog title="打分" v-model="scoreModal" width="1000px" append-to-body>
      <el-descriptions title="基本信息">
        <el-descriptions-item label="填写人账号">{{ scoreInfo.userCode }}</el-descriptions-item>
        <el-descriptions-item label="填写人名称">{{ scoreInfo.userName }}</el-descriptions-item>
        <el-descriptions-item label="提交时间">{{ scoreInfo.submitTime }}</el-descriptions-item>
        <el-descriptions-item label="试卷">{{ scoreInfo.paperTitle }}</el-descriptions-item>
      </el-descriptions>
      <el-card shadow="never">
        <template #header>答题明细</template>
        <div style="max-height: 650px;overflow-y: auto; overflow-x: hidden">
          <ol>
            <li v-for="(item, index) in scoreInfo.examQuestionList" :key="index + 'q'" class="question-item">
              <div class="question-title">
                {{ item.questionText }}({{ item.score }}分)
                <el-input-number v-if="item.examAnswer" v-model="item.examAnswer.score" :min="0" style="float: right">
                  <template #suffix>
                    <span>分</span>
                  </template>
                </el-input-number>
              </div>
              <div v-if="item.questionType === 'CHOICE'">
                <el-radio-group v-model="item.selectedOptionId" disabled>
                  <el-radio v-for="(option, i) in item.examQuestionOptionList" :key="option.id" :value="option.id">
                    {{ ['A', 'B', 'C', 'D', 'E'][i]}}.
                    {{ option.optionText }}
                    <span v-if="option.isCorrect" class="color-green">(正确答案)</span>
                  </el-radio>
                </el-radio-group>
              </div>
              <div v-if="item.questionType === 'INPUT'">
                &nbsp;{{ item.inputContent }}
              </div>
              <hr>
            </li>
          </ol>
        </div>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitScore">确 定</el-button>
        <el-button @click="scoreModal = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAnswer, getAnswer, delAnswer, addAnswer, updateAnswer } from "@/api/system/answer"
import { listPaper, getPaper } from "@/api/system/paper"
import SmartTable from '@/components/SmartTable/index.vue'

export default {
  name: "Answer",
  components: {SmartTable},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selectedList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 答题记录表格数据
      answerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paperId: null,
        questionId: null,
        selectedOptionId: null,
        inputContent: null,
        userCode: null,
        userName: null,
        phonenumber: null,
        submitTime: null,
        score: null
      },

      detailModal: false,
      detailInfo: { examQuestionList: []},

      scoreModal: false,
      scoreInfo: { examQuestionList: []},
    }
  },
  created() {
    this.getList()
  },
  activated() {
    this.getList()
  },
  methods: {
    /** 查询答题记录列表 */
    getList() {
      this.loading = true
      listAnswer(this.queryParams).then(response => {
        this.answerList = response.rows.map(item => {
          return {
            ...item,
            key: item.id + item.userCode + item.submitTime,
          }
        })
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedList = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: 'answer/add',
      })
    },
    /** 查看详情 */
    handleDetail(row) {
      const id = row.id || this.ids
      getAnswer({
        id, userCode: row.userCode, submitTime: row.submitTime
      }).then(res => {
        const examQuestionList = res.data.examQuestionList.map(item => {
          return {
            ...item,
            inputContent: item.examAnswer? item.examAnswer.inputContent : '',
            selectedOptionId: item.examAnswer? item.examAnswer.selectedOptionId : null,
          }
        })
        this.detailInfo = {
          ...row,
          examQuestionList,
        }
        this.detailModal = true;
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postData = this.selectedList.map(item => {
        return {
          id: item.id, userCode: item.userCode, submitTime: item.submitTime,
        }
      })
      this.$modal.confirm('是否确认删除答题记录？').then(function() {
        return delAnswer(postData)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/answer/export', {
        ...this.queryParams
      }, `answer_${new Date().getTime()}.xlsx`)
    },

    handleScore(row) {
      const id = row.id || this.ids
      getAnswer({
        id, userCode: row.userCode, submitTime: row.submitTime
      }).then(res => {
        const examQuestionList = res.data.examQuestionList.map(item => {
          return {
            ...item,
            inputContent: item.examAnswer? item.examAnswer.inputContent : '',
            selectedOptionId: item.examAnswer? item.examAnswer.selectedOptionId : null,
          }
        })
        this.scoreInfo = {
          ...row,
          examQuestionList,
        }
        this.scoreModal = true;
      })
    },
    submitScore() {
      const examAnswerList = this.scoreInfo.examQuestionList.map(item => {
        return {
          id: item.examAnswer?.id,
          score: item.examAnswer?.score,
        };
      });
      updateAnswer(examAnswerList).then(res => {
        this.$modal.msgSuccess("打分成功")
        this.scoreModal = false
        this.getList()
      })
    }
  }
}
</script>
<style scoped>
  .question-item {
    margin-bottom: 12px;
  }
  .question-title {
    font-size: 16px;
    margin-bottom: 8px;
  }
  .color-green {
    color: yellowgreen;
  }
</style>
